#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node
{
    char data[10];
    struct node *left, *right;
} BTree;

void SetData(BTree **root, const char data[])
{
	(*root) = (BTree *)malloc(sizeof(BTree));
	(*root)->data[0] = data[0];
	(*root)->data[1] = '\0';
	(*root)->left = NULL;
	(*root)->right = NULL;
}

void BtreeToExp(BTree * root, int deep)
{
	if (root == NULL) { 
		return;
	}
	else if (root->left == NULL && root->right == NULL) {
		printf("%s", root->data);
	}
	else {
		if (deep > 1) printf("(");
		BtreeToExp(root->left, deep + 1); 
		printf("%s", root->data); 
		BtreeToExp(root->right, deep + 1);
		if (deep > 1) printf(")");
	}
}

void BtreeToE(BTree *root) 
{
	BtreeToExp(root, 1); 
}

int main(int argc, char const *argv[])
{
	BTree *root;

	SetData(&root, "*");
	SetData(&root->left, "+");
	SetData(&root->left->left,  "a");
	SetData(&root->left->right, "b");

	SetData(&root->right, "*");
	SetData(&root->right->left,  "c");
	SetData(&root->right->right, "-");
	SetData(&root->right->right->right, "d");

	BtreeToE(root);
	printf("\n");

	BTree *root2;

	SetData(&root2, "+");
	SetData(&root2->left, "*");
	SetData(&root2->left->left,  "a");
	SetData(&root2->left->right, "b");

	SetData(&root2->right, "-");
	SetData(&root2->right->right, "-");
	SetData(&root2->right->right->left,  "c");
	SetData(&root2->right->right->right, "d");

	BtreeToE(root2);
	printf("\n");
	return 0;
}